package com.itheima.ydd.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itheima.ydd.common.R;
import com.itheima.ydd.common.ResultData;
import com.itheima.ydd.dto.SysConfigDto;
import com.itheima.ydd.entity.Sys;
import com.itheima.ydd.entity.SysConfig;
import com.itheima.ydd.mapper.SysConfigMapper;
import com.itheima.ydd.service.ISysConfigService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2022-05-22
 */
@RestController
@RequestMapping("/sys_config")
@CrossOrigin
@Slf4j
public class SysConfigController {
    @Autowired
    private ISysConfigService iSysConfigService;

    @Autowired
    private SysConfigMapper mapper;

    @GetMapping("all")
    public ResultData all(){
        //基础配置
        //封装站点配置
        Map<String,Object> siteMap =new LinkedHashMap<>();
        //封装底部信息
        Map<String,Object> bottomMap =new LinkedHashMap<>();
        //封装其他配置
        Map<String,Object> otherMap =new LinkedHashMap<>();

        //从表中获取站点配置数据，并封装
        LambdaQueryWrapper<SysConfig> siteLqw = new LambdaQueryWrapper<>();
        siteLqw.eq(SysConfig::getGroupValue,"站点配置");
        List<SysConfig> siteConfig = iSysConfigService.list(siteLqw);
        //sysConfigDtoList存放站点配置数据
        List<SysConfigDto> sysConfigDtoList = new ArrayList<>();
        for (SysConfig sysConfig : siteConfig) {
            SysConfigDto dto =new SysConfigDto();
            BeanUtils.copyProperties(sysConfig,dto);
            LinkedList<Map> list=new LinkedList<>();
            if (StringUtils.isNotEmpty(sysConfig.getConfigValue())){
                String[] split = sysConfig.getConfigValue().split(("&"));
                for (String s : split) {
                    Map<String,Object> map =new HashMap<>();
                    String[] split1 = s.split("=");
                    map.put("label",split1[0]);
                    map.put("value",split1[1]);
                    list.add(map);
                }
            }
            dto.setConfig_value(list);
            sysConfigDtoList.add(dto);
        }
        siteMap.put("name","站点配置");
        siteMap.put("data",sysConfigDtoList);

        //从表中获取底部信息数据并封装
        LambdaQueryWrapper<SysConfig> bottomLqw = new LambdaQueryWrapper<>();
        bottomLqw.eq(SysConfig::getGroupValue,"底部信息");
        List<SysConfig> bottomInfo = iSysConfigService.list(bottomLqw);
        //bottomInfoDto存放底部信息数据
        List<SysConfigDto> bottomInfoDto = new ArrayList<>();
        for (SysConfig sysConfig : bottomInfo) {
            SysConfigDto dto =new SysConfigDto();
            BeanUtils.copyProperties(sysConfig,dto);
            bottomInfoDto.add(dto);
        }
        bottomMap.put("name","底部信息");
        bottomMap.put("data",bottomInfoDto);

        //从表中获取其他配置数据
        LambdaQueryWrapper<SysConfig> otherLqw = new LambdaQueryWrapper<>();
        otherLqw.eq(SysConfig::getGroupValue,"其他配置");
        List<SysConfig> otherConfig = iSysConfigService.list(otherLqw);
        //otherConfigDto存放其他配置数据
        List<SysConfigDto> otherConfigDto = new ArrayList<>();
        for (SysConfig sysConfig : otherConfig) {
            SysConfigDto dto =new SysConfigDto();
            BeanUtils.copyProperties(sysConfig,dto);
            otherConfigDto.add(dto);
        }
        otherMap.put("name","其他配置");
        otherMap.put("data",otherConfigDto);

        //将基础配置数据封装到list中作为一个整体，存放如map集合中
        List<Map> basicList = new ArrayList<>();
        basicList.add(siteMap);
        basicList.add(bottomMap);
        basicList.add(otherMap);

        Map<String , Object> baseMap =new LinkedHashMap<>();
        baseMap.put("name","基础配置");
        baseMap.put("key","base");
        baseMap.put("children",basicList);

        //系统设置-->管理系统配置
        LambdaQueryWrapper<SysConfig> manageLqw = new LambdaQueryWrapper<>();
        manageLqw.eq(SysConfig::getGroupValue,"管理系统配置");
        List<SysConfig> manageConfig = iSysConfigService.list(manageLqw);
        Map<String,Object> manageMap =new LinkedHashMap<>();
        manageMap.put("name","管理系统配置");
        manageMap.put("data",manageConfig);

        List<Map> sysList = new ArrayList<>();
        sysList.add(manageMap);

        Map<String , Object> sysMap =new LinkedHashMap<>();
        sysMap.put("name","系统设置");
        sysMap.put("key","system");
        sysMap.put("children",sysList);

        //其他设置-->通知公告
        LambdaQueryWrapper<SysConfig> rptLqw = new LambdaQueryWrapper<>();
        rptLqw.eq(SysConfig::getGroupValue,"通知公告");
        List<SysConfig> rpt = iSysConfigService.list(rptLqw);
        Map<String,Object> rptMap =new LinkedHashMap<>();
        rptMap.put("name","通知公告");
        rptMap.put("data",rpt);

        //其他设置-->前台图片
        LambdaQueryWrapper<SysConfig> imaLqw = new LambdaQueryWrapper<>();
        imaLqw.eq(SysConfig::getGroupValue,"前台图片");
        List<SysConfig> image = iSysConfigService.list(imaLqw);
        Map<String,Object> imaMap =new LinkedHashMap<>();
        imaMap.put("name","前台图片");
        imaMap.put("data",image);

        List<Map> otherList = new ArrayList<>();
        otherList.add(rptMap);
        otherList.add(imaMap);

        Map<String,Object> otherConfigMap =new LinkedHashMap<>();
        otherConfigMap.put("name","其他设置");
        otherConfigMap.put("key","custom");
        otherConfigMap.put("children",otherList);

        //配置数据封装
        List<Map> configList = new ArrayList<>();
        configList.add(baseMap);
        configList.add(sysMap);
        configList.add(otherConfigMap);

        //alldata封装
        QueryWrapper<SysConfig> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("id","attr_key","attr_value","validator","config_value","type");
        List<SysConfig> allData =mapper.selectList(queryWrapper);
        //新建一个类封装allData数据
        List<Sys> sysData =new ArrayList<>();
        for (SysConfig datum : allData) {
            Sys sys=new Sys();
            sys.setId(datum.getId());
            sys.setAttrKey(datum.getAttrKey());
            sys.setAttrValue(datum.getAttrValue());
            sys.setType(datum.getType());
            sys.setConfigValue(datum.getConfigValue());
            sys.setValidator(datum.getValidator());
            sysData.add(sys);
        }

        //configForm和configRules封装
        List<SysConfig> sysConfigList = iSysConfigService.list();
        Map<String,String> configMap = new LinkedHashMap<>();
        Map<String,String> rulesMap = new LinkedHashMap<>();
        for (SysConfig sysConfig : sysConfigList) {
            String attrKey = sysConfig.getAttrKey();
            String attrValue = sysConfig.getAttrValue();
            String validator = sysConfig.getValidator();
            configMap.put(attrKey,attrValue);
            if (StringUtils.isNotEmpty(validator)){
                rulesMap.put(attrKey,validator);
            }
        }

        //所有返回数据封装
        Map<String,Object> sysConfigMap = new LinkedHashMap<>();
        sysConfigMap.put("list",configList);
        sysConfigMap.put("allData",sysData);
        sysConfigMap.put("configForm",configMap);
        sysConfigMap.put("configRules",rulesMap);

        return ResultData.ok("",sysConfigMap);
    }


    @PostMapping("update")
    public ResultData update(@RequestBody List<SysConfig> list){
        iSysConfigService.updateBatchById(list);
        return ResultData.ok("更新成功","");
    }

}
